import { Component, OnInit, ViewChild } from "@angular/core";
import { NzMessageService } from "ng-zorro-antd/message";
import { Router } from "@angular/router";
import { SystemConfigService } from "./system-config.service";
import { SystemConfigEditComponent } from "./system-config-edit/system-config-edit.component";
import { NzModalService } from "ng-zorro-antd/modal";

@Component({
  selector: 'app-system-config',
  templateUrl: './system-config.component.html',
  styles: [
  ]
})
export class SystemConfigComponent implements OnInit {

  checked = false;
  indeterminate = false;

  listOfCurrentPageData = [];
  setOfCheckedId = new Set<number>();

  loading = false;

  // 列表数据
  dataList = [];

  // 查询参数
  searchBeans = [{
    attributeName: "keyCode",
    searchType: "LIKE",
    attributeValue: ""
  }];
  // 分页参数
  pageData = { page: 0, size: 10, total: 100 };

  @ViewChild("systemConfigEditComponent", { static: false })
  public systemConfigEditComponent: SystemConfigEditComponent;

  constructor(private systemConfigService: SystemConfigService,
              private msgSrv: NzMessageService,
              private modal: NzModalService,
              private router: Router,
  ) {
  }

  ngOnInit(): void {
    this.loadListData();
  }

  dictItem(id){
    this.router.navigateByUrl(`sys/dict-item/${id}`);
  }

  delete(id?) {

    this.modal.confirm({
      nzTitle: '确定删除吗?',
      nzOkText: 'Yes',
      nzOkType: 'danger',
      nzOnOk: () => {
        this.systemConfigService.delete(id).subscribe((res) => {
          if (res.code === 1) {
            this.msgSrv.success('删除完成');
            this.loadListData();
          } else {
            this.msgSrv.error(res.message);
          }
        });
      },
      nzCancelText: 'No',
    });

  }

  /**
   * 清除查询参数
   */
  clearParam() {

    this.searchBeans.forEach((item) => {
      item.attributeValue = "";
    });
  }

  onSmt() {
    this.loadListData();
  }

  cancel() {
    this.msgSrv.success("已取消");
  }

  openAdd() {
    this.systemConfigEditComponent.open();
  }

  openEdit(record): void {

    console.log(record);
    this.systemConfigEditComponent.edit(record);

  }

  /**
   * 加载列表数据
   */
  loadListData() {

    this.loading = true;

    const searchData = {
      page: this.pageData.page,
      size: this.pageData.size,
      searchFilter: {
        searchBeans: this.searchBeans
      }
    };

    this.systemConfigService.findPage(searchData).subscribe((res) => {

      this.loading = false;

      if (res.code === 1) {
        this.pageData.page = res.data.number;
        this.pageData.size = res.data.size;
        this.pageData.total = res.data.totalElements;
        this.dataList = res.data.content;
      } else {
        this.msgSrv.error(res.message);
      }

    });
  }

  onAllChecked(checked: boolean): void {
    this.listOfCurrentPageData.filter(({ disabled }) => !disabled).forEach(({ id }) => this.updateCheckedSet(id, checked));
    this.refreshCheckedStatus();
  }

  updateCheckedSet(id: number, checked: boolean): void {
    if (checked) {
      this.setOfCheckedId.add(id);
    } else {
      this.setOfCheckedId.delete(id);
    }
  }

  refreshCheckedStatus(): void {
    const listOfEnabledData = this.listOfCurrentPageData.filter(({ disabled }) => !disabled);
    this.checked = listOfEnabledData.every(({ id }) => this.setOfCheckedId.has(id));
    this.indeterminate = listOfEnabledData.some(({ id }) => this.setOfCheckedId.has(id)) && !this.checked;
  }

  onItemChecked(id: number, checked: boolean): void {
    this.updateCheckedSet(id, checked);
    this.refreshCheckedStatus();
  }

  _onReuseInit() {
    this.loadListData();
  }

}
